package com.hongxun.design.fragments;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.adapters.MyAdapter;
import com.hongxun.design.bean.ConsumerInfo;
import com.hongxun.design.bean.MyListViewItemBean;
import com.hongxun.design.constant.Constant;
import com.hongxun.design.listeners.ListenerSet;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.MyLog;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.utils.DataUtil;
import com.hongxun.design.utils.FragmentUtil;
import com.hongxun.design.utils.TimeUtil;
import com.hongxun.design.utils.fastjson.FastJsonUtil;
import com.hongxun.design.utils.httputil.HttpStringUtil;
import com.hongxun.design.utils.imageutil.UriUtil;

import java.util.List;

public class MyFragment extends BaseFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View settingLayout = inflater.inflate(R.layout.fragment_mylayout,
                container, false);


        findView(settingLayout);
        return settingLayout;
    }


    @Override
    public void onResume() {
        super.onResume();
        FragmentUtil.currFragTag = Constant.FRAGMENT_FLAG_MY;
        baseFragment = this;
        if (MyApp.isgetMessage) {
            mymessage_num.setVisibility(View.VISIBLE);
        } else {
            mymessage_num.setVisibility(View.GONE);
        }
    }

    @Override
    public void Refresh() {
        super.Refresh();
        if (MyApp.isOnLine) {
            ConsumerInfo();
        } else {
            setInfo();
        }

    }

    TextView username, user_role, user_company, mymessage_num;
    LinearLayout user_role_company;
    SimpleDraweeView login_icon;
    TextView isvip, viptime;

    @Override
    void findView(View v) {
        super.findView(v);
        /**初始化控件**/
        RelativeLayout user_item = (RelativeLayout) v.findViewById(R.id.my_user_item);
        RelativeLayout mymessage_button = (RelativeLayout) v.findViewById(R.id.mymessage_button);
        RelativeLayout myfoot_button = (RelativeLayout) v.findViewById(R.id.myfoot_button);
        RelativeLayout mycollect_button = (RelativeLayout) v.findViewById(R.id.mycollect_button);


        isvip = (TextView) v.findViewById(R.id.isvip);
        viptime = (TextView) v.findViewById(R.id.viptime);

        TextView mymessage_icon = (TextView) v.findViewById(R.id.mymessage_icon);
        TextView myfoot_icon = (TextView) v.findViewById(R.id.myfoot_icon);
        TextView mycollect_icon = (TextView) v.findViewById(R.id.mycollect_icon);

        username = (TextView) v.findViewById(R.id.username);
        user_role = (TextView) v.findViewById(R.id.user_role);
        user_company = (TextView) v.findViewById(R.id.user_company);
        mymessage_num = (TextView) v.findViewById(R.id.mymessage_num);
        user_role_company = (LinearLayout) v.findViewById(R.id.user_role_company);
        login_icon = (SimpleDraweeView) v.findViewById(R.id.login_icon);
        setInfo();
        SetTypeface.set(mymessage_icon, IconSourse.message);
        SetTypeface.set(myfoot_icon, IconSourse.foot);
        SetTypeface.set(mycollect_icon, IconSourse.Favorites);

        /**加载栏目**/
        ListView mylistview = (ListView) v.findViewById(R.id.mylistview);
        List<MyListViewItemBean> data = DataUtil.getMyListData();
        MyAdapter adapter = new MyAdapter(getActivity(), data);
        mylistview.setAdapter(adapter);
        /**设置点击监听**/
        user_item.setOnClickListener(ListenerSet.founction_userset);
        mymessage_button.setOnClickListener(ListenerSet.founction_mynews);
        myfoot_button.setOnClickListener(ListenerSet.founction_projectfoot);
        mycollect_button.setOnClickListener(ListenerSet.founction_collect);
    }

    /**
     * 设置基本信息*
     */
    private void setInfo() {
        if (username == null) return;
        MyLog.L(MString.SUCCESSED + " url=" + UriUtil.getUri(MyApp.userInfo.getAvatar()));
        login_icon.setImageURI(UriUtil.getUri(MyApp.userInfo.getAvatar()));
        username.setText(MyApp.userInfo.getRealname());
        String s = MyApp.userInfo.getRole();
        String Company = MyApp.userInfo.getCompany();
        if (s.length() > 0) {
            user_role.setText(s);
            user_role.setVisibility(View.VISIBLE);
        } else {
            user_role.setVisibility(View.GONE);
        }
        user_company.setText(Company);
        if (s.length() == 0 && Company.length() == 0) {
            user_role_company.setVisibility(View.GONE);
        } else {
            user_role_company.setVisibility(View.VISIBLE);
        }
        if (MyApp.isgetMessage) {
            mymessage_num.setVisibility(View.VISIBLE);
        } else {
            mymessage_num.setVisibility(View.GONE);
        }

        if (MyApp.userInfo.getOpenIdent() != null && MyApp.userInfo.getOpenIdent().length() > 0) {
            isvip.setVisibility(View.VISIBLE);
            viptime.setVisibility(View.VISIBLE);
            if (MyApp.userInfo.getIs_vip() == 0) {
                //非vip
                isvip.setVisibility(View.GONE);
                viptime.setVisibility(View.GONE);
            } else {
                //vip
                if (MyApp.userInfo.getCurrent_date() >= MyApp.userInfo.getVip_start() && MyApp.userInfo.getCurrent_date() <= MyApp.userInfo.getVip_end()) {
                    //未过期
                    isvip.setBackgroundResource(R.drawable.c18_white_item);
                    isvip.setTextColor(getResources().getColor(R.color.login_yellow_m));
                    isvip.setText("已授权");
                } else {
                    //已过期
                    isvip.setBackgroundResource(R.drawable.c18_gray_item);
                    isvip.setTextColor(getResources().getColor(R.color.grayb4));
                    isvip.setText("授权过期");
                }
                viptime.setText("授权期限："+TimeUtil.getyyyyMMdd(MyApp.userInfo.getVip_start()+"000")+"\t至\t"+TimeUtil.getyyyyMMdd(MyApp.userInfo.getVip_end()+"000"));

            }
        } else {
            isvip.setVisibility(View.GONE);
            viptime.setVisibility(View.GONE);
        }
    }


    private void ConsumerInfo() {
        MyApp.HTTP.ConsumerInfo(handler, 1);
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    if (FastJsonUtil.JsonToCheck((String) msg.obj)) {
                        ConsumerInfo info = FastJsonUtil.JsonToLoginData((String) msg.obj);
                        MyApp.userInfo.clearThis();
                        MyApp.userInfo.setBean(info);
                    }
                    setInfo();
                    break;
                case HttpStringUtil.ERROR:
                    setInfo();
                    break;
            }
        }
    };

    @Override
    void initialization() {
        super.initialization();
    }


}
